<%-- 
    Document   : BlockVerify
    Created on : Nov 16, 2014, 7:38:18 PM
    Author     : user
--%>

<%-- 
    Document   : VerifyFile
    Created on : Oct 26, 2014, 12:44:20 PM
    Author     : user
--%>


<%@page import="newpackage.CountWord"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="newpackage.CompareFileContents"%>
<%@page import="newpackage.compareFileContent"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="newpackage.ConnectionFactory"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
<script>
    function showmsg()
    {
        alert(document.getElementById('result').value);
    }
function myFunction(name) 
{
 //   var secret_key = prompt("Please enter your secret key", "Please enter your secret key");
  //  var block = prompt("Please enter block name", "Block no");
  //  if (secret_key != null && block!=null) 
 //   {
       //    var name=document.getElementById("file_name").value;
           
           document.forms["verify_file"].submit();
   // }
}
</script>
</head>
<body background="image.jpg">
    <h1> ENSURING PRIVACY AND DATA FRESHNESS OF SHARED DATA IN CLOUD </h1>
    <center>
    <form id="verify_file" name="verify_file" >
    <%! ArrayList<String> getFileNames()throws Exception
         {
         ArrayList<String>  names=new ArrayList<String>();
           Connection con=ConnectionFactory.getConnection();
           Statement select=con.createStatement();
           String Query="SELECT file_details.filename FROM file_details ";
           ResultSet rs=select.executeQuery(Query);
           int i=0;
           while(rs.next())
           {
               String s=rs.getString(1);
               names.add(s);
           }
           return names;
        }
      %>
     <%
     ArrayList<String> files=getFileNames();
        %>
        <p>
            Select file Name:
        </p>
        
        <select name="file_name"  id="file_name">
         <option>select file</option>
        <%
        if(request.getParameter("file_name")!=null)
        {
    %>
        <option value="<%=request.getParameter("file_name")%>" selected="" ><%=request.getParameter("file_name").substring(request.getParameter("file_name").lastIndexOf('/')+1)%></option>
          <%  
        }
        for(String name:files)
        {
        if(name.equals(request.getParameter("file_name")))
        continue;
        %>
    <option value="<%=name%>" ><%=name.substring(name.lastIndexOf('/')+1)%></option>
        <%
        }
        
        
        %>
        
        </select>
        <br/>
        
        Block 1:<input type="radio" name="block" value="part0"/><br/>
        
        Block 2:  <input type="radio" name="block" value="part1"/><br/>
        
        Block 3: <input type="radio" name="block" value="part2"/><br/>
        
        <input type="hidden" name="block_name" id="block_name"/>
        <input type="hidden" name="secret_key" id="secret_key"/>
  
        <%
        
        String block_name=request.getParameter("block");
      
        String file_name=request.getParameter("file_name");
        boolean compare=false;
        if(block_name!=null&&file_name!=null)
        {
          //  compare=compareFileContent.compare(file_name, file_name.replace("main","sub"));
          //  compare=CompareFileContents.contentEquals(new FileInputStream(file_name),new FileInputStream(file_name.replace("main","sub")));
            String first_block=file_name+"."+block_name;
            String second_block=(file_name+"."+block_name).replace("main","sub");
            int c1=CountWord.getCount(first_block);
            int c2=CountWord.getCount(second_block);
            if(c1==c2)
        {
           compare=true;
        }
        if(compare)
            
            {
         
            out.print("Block is safe");
            out.println("<input type='hidden' name='result' value='Block is safe' onload='showmsg(this)'/>"); 
            out.println("<input type='hidden' name='file_name' value='"+file_name+"'/>"); 
          
            }
            
            else
            {
                
           
           out.println("<input type='hidden' name='result' value='Block is Modified' onload='showmsg(this)'/>"); 
           out.println("<input type='hidden' name='result' value='"+file_name+"'/>"); 
           out.print("Block is Modified");
        
               }
            
         }
        
        
%>

<input type="button" value="check" onclick="myFunction();" />
<input type="submit" formmethod="post" formaction="Update.jsp"  value="Update"/>
<input type="submit" formmethod="post" formaction="Revert.jsp"  value="Revert"/>
<a href="Login.jsp">LogOut</a>

<a href="FileUpload.jsp">Back</a>
          </form>
</center>
</body>
</html>
